

#include "catch.hpp"

#include <cpp/cache.hpp>


TEST_CASE("cache")
{

	cpp::cache<int, long> c(5);

	c.set(1000, 1000);


	REQUIRE(c.get(1000));



	for(int i = 0; i < 100; i++)
	{
		c.set(i, i);
	}


	REQUIRE(!c.contains(100));


	REQUIRE(c.size() == 5);



	REQUIRE(c.get(95));
	REQUIRE(c.get(96));
	REQUIRE(c.get(97));
	REQUIRE(c.get(98));
	REQUIRE(c.get(99));

	c.set(0, 0);

	REQUIRE(!c.contains(95));
}

TEST_CASE("cache2")
{

	cpp::cache<int, int> cache_lru(1);
	cache_lru.set(7, 777);
	REQUIRE(cache_lru.contains(7));
	REQUIRE(777 == cache_lru.get(7));
	REQUIRE(1 == cache_lru.size());
}

TEST_CASE("cache3")
{
	cpp::cache<int, int> cache_lru(1);
	REQUIRE(!cache_lru.get(7));
}


const int NUM_OF_TEST2_RECORDS = 100;
const int TEST2_CACHE_CAPACITY = 50;

TEST_CASE("cache4") {
	cpp::cache<int, int> cache_lru(TEST2_CACHE_CAPACITY);

	for(int i = 0; i < NUM_OF_TEST2_RECORDS; ++i) {
		cache_lru.set(i, i);
	}

	for(int i = 0; i < NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; ++i) {
		REQUIRE_FALSE(cache_lru.contains(i));
	}

	for(int i = NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; i < NUM_OF_TEST2_RECORDS; ++i) {
		REQUIRE(cache_lru.contains(i));
		REQUIRE(i == cache_lru.get(i));
	}

	size_t size = cache_lru.size();
	REQUIRE(TEST2_CACHE_CAPACITY == size);
}



TEST_CASE("cache5") {
	cpp::cache<int, int> cache_lru(TEST2_CACHE_CAPACITY, std::chrono::seconds(1));

	for(int i = 0; i < NUM_OF_TEST2_RECORDS; ++i) {
		cache_lru.set(i, i);
	}

	for(int i = 0; i < NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; ++i) {
		REQUIRE_FALSE(cache_lru.contains(i));
	}

	std::this_thread::sleep_for(std::chrono::seconds(2));

	for(int i = NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY; i < NUM_OF_TEST2_RECORDS; ++i) {
		REQUIRE(cache_lru.contains(i));
		REQUIRE_FALSE(cache_lru.get(i));
	}

	size_t size = cache_lru.size();
	REQUIRE(0 == size);
}
